package com.mud.dnd.ejb;

import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionManagement;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.model.User;
import com.mud.dnd.domain.model.User.Role;
import com.mud.dnd.ejb.common.AuthenticationException;
import com.mud.dnd.ejb.services.AuthenticationService;

/**
 * Session Bean implementation class UserManager
 */
@Stateless
@TransactionManagement
public class UserManager {
	
	@EJB
	private AuthenticationService userService;
	

    /**
     * Default constructor. 
     */
    public UserManager() {
        // TODO Auto-generated constructor stub
    }
    
    @TransactionAttribute
    public String loginUser(String name, String password) throws DAOException, AuthenticationException
    {
    	
    	return userService.loginUser(name, password);
    }
    
    @TransactionAttribute
    public int createUser(String name, String password) throws DAOException, AuthenticationException
    {
    	return userService.createUser(name, password, Role.User);
    }
    
 
    @TransactionAttribute
	public void deleteUser(String name, String targetName, String sessionID) throws DAOException, AuthenticationException
    {
    	User user = userService.getUserData(name);
    	if(!userService.checkSessionForUser(name, sessionID))
    	{
    		throw new AuthenticationException("Authentication error.");
    	}
    	
    	if(user.getRole()!= Role.Admin)
    	{
    		throw new AuthenticationException("Authentication error.");
    	}
    	
    	User targetUser = userService.getUserData(targetName);
    	userService.deleteUser(targetUser);
    	
    }
    
    @TransactionAttribute
    public void logoutUser(String name, String sessionID) throws DAOException, AuthenticationException
    {
    	User user = userService.getUserData(name);
    	if(!userService.checkSessionForUser(name, sessionID))
    	{
    		throw new AuthenticationException("Authentication error.");
    	}
    	
    	userService.clearSessionforUser(user);	
    }
    
    public boolean checkIfUserAdmin(String name) throws DAOException
    {
    	User userData = userService.getUserData(name);
    	
    	if(userData.getRole()==Role.Admin)
    	{
    		return true;
    	}else
    	{
    		return false;
    	}
    }
    
    @TransactionAttribute
    public int createAdminUser(String name, String password, String userName, String sessionID) throws DAOException, AuthenticationException
    {
    	//FIXME nem eleg ez itt
    	
    	
    	if(!userService.checkSessionForUser(userName, sessionID))
    	{
    		throw new AuthenticationException("Authentication error.");
    	}
    	
    	return userService.createUser(name, password, Role.Admin);
    }
    
    public boolean checkSessionForUser(String name, String sessionID) throws DAOException
    {
    	return userService.checkSessionForUser(name, sessionID);
    	
    }
    
    
    
    @Schedule(minute = "*/5", second="0")
    @TransactionAttribute
    public void clearExpiredSessions()
    {
    	userService.clearExpiredSessions();
    }
    

}
